package com.yuqing.sentimentapp.login.presenter.impl;

import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.login.acitivity.VerifyCodeActivity;
import com.yuqing.sentimentapp.login.bean.LoginResultBean;
import com.yuqing.sentimentapp.login.module.LoginModule;
import com.yuqing.sentimentapp.login.presenter.LoginPresenter;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ShenHe on 2019/4/10.
 */

public class LoginPresenterImpl implements LoginPresenter {

    private VerifyCodeActivity verifyCodeActivity;
    private LoginModule loginModule;

    public LoginPresenterImpl(VerifyCodeActivity verifyCodeActivity) {
        this.verifyCodeActivity = verifyCodeActivity;
        loginModule = new LoginModule();
    }

    @Override
    public void loginRequest(String phone, String verifyCode) {
        loginModule.requestLoginResult(verifyCodeActivity, phone, verifyCode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<LoginResultBean>() {

                    @Override
                    protected void hideDialog() {
                        verifyCodeActivity.stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        verifyCodeActivity.startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(LoginResultBean loginResultBean) {
                        if (checkContext()) return;
                        verifyCodeActivity.onSuccess(loginResultBean);
                    }

                    @Override
                    protected void onFailure(String message) {
                        verifyCodeActivity.onFailure(message);
                    }
                });
    }

    private boolean checkContext() {
        if (verifyCodeActivity.isFinishing() || verifyCodeActivity.isDestroyed())
            return true;
        return false;
    }
}
